//
//  STStatusToolBarView.swift
//  STVVeibo
//
//  Created by leergou on 16/7/24.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

import UIKit

class STStatusToolBarView: UIView {
    
    // 定义三个按钮属性,用于将创建按钮 和 按钮 设置
    // 转发按钮
    var retweetButton: UIButton?
    // 评论按钮
    var commentButton: UIButton?
    // 赞
    var unlikeButton: UIButton?
    
    // 定义一个模型属性,供外界赋值
    var statusViewModel: STStatusViewModel?{
        
        didSet{
            // 赋值
            retweetButton?.setTitle(statusViewModel?.repostsCount, forState: UIControlState.Normal)
            commentButton?.setTitle(statusViewModel?.commentsCount, forState: UIControlState.Normal)
            unlikeButton?.setTitle(statusViewModel?.attitudesCount, forState: UIControlState.Normal)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: setupUI
    func setupUI(){
        // 添加子控件
        retweetButton = addChildButton("timeline_icon_retweet", title: "转发")
        commentButton = addChildButton("timeline_icon_comment", title: "评论")
        unlikeButton = addChildButton("timeline_icon_unlike", title: "赞")
        
        let leftTimeLine = addChildLine()
        let rightTimeLine = addChildLine()
        
        // 添加约束
        //MAKR: 这里学习 如何快速约束 多个控件等宽布置
        retweetButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.leading.equalTo(self)
            make.width.equalTo(commentButton!)
        }
        
        commentButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(retweetButton!.snp_trailing)
            make.width.equalTo(unlikeButton!)
        }
        
        unlikeButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(commentButton!.snp_trailing)
        }
        
        leftTimeLine.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self)
            make.centerX.equalTo(retweetButton!.snp_trailing)
        }
        
        rightTimeLine.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self)
            make.centerX.equalTo(commentButton!.snp_trailing)
        }
        
    }
    
    //MARK: 创建分割线函数
    func addChildLine() -> UIImageView{
        
        let lineImg = UIImageView(imgName: "timeline_card_bottom_line")
        addSubview(lineImg)
        return lineImg
    }
    
    
    //MARK: 创建 button 的函数
    func addChildButton(imgName: String, title: String) -> UIButton{
        
        let button = UIButton(title: title, titleColro: UIColor.darkGrayColor(), font: 15, target: nil, action: nil)
        
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        
        addSubview(button)
        
        return button
    }
}
















